/* ESP32C3 UART Driver Header */

#ifndef __UART_H__
#define __UART_H__

#include <stdint.h>
#include <stddef.h>

/* UART port definition */
typedef enum {
    UART_NUM_0 = 0,
    UART_NUM_1,
    UART_NUM_MAX
} uart_num_t;

/* UART baud rate */
typedef enum {
    UART_BAUD_RATE_9600 = 9600,
    UART_BAUD_RATE_19200 = 19200,
    UART_BAUD_RATE_38400 = 38400,
    UART_BAUD_RATE_57600 = 57600,
    UART_BAUD_RATE_115200 = 115200,
    UART_BAUD_RATE_230400 = 230400,
    UART_BAUD_RATE_460800 = 460800,
    UART_BAUD_RATE_921600 = 921600
} uart_baud_rate_t;

/* UART data bits */
typedef enum {
    UART_DATA_5_BITS = 5,
    UART_DATA_6_BITS = 6,
    UART_DATA_7_BITS = 7,
    UART_DATA_8_BITS = 8
} uart_data_bits_t;

/* UART parity */
typedef enum {
    UART_PARITY_DISABLE = 0,
    UART_PARITY_EVEN,
    UART_PARITY_ODD
} uart_parity_t;

/* UART stop bits */
typedef enum {
    UART_STOP_BITS_1 = 1,
    UART_STOP_BITS_1_5 = 2,
    UART_STOP_BITS_2 = 3
} uart_stop_bits_t;

/* UART configuration */
typedef struct {
    uart_baud_rate_t baud_rate;
    uart_data_bits_t data_bits;
    uart_parity_t parity;
    uart_stop_bits_t stop_bits;
} uart_config_t;

/* UART functions */
void uart_init(uart_num_t uart_num, uart_config_t *config);
void uart_write_bytes(uart_num_t uart_num, const uint8_t *data, size_t size);
void uart_write_char(uart_num_t uart_num, uint8_t ch);
size_t uart_read_bytes(uart_num_t uart_num, uint8_t *data, size_t size, uint32_t timeout_ms);
int uart_read_char(uart_num_t uart_num, uint32_t timeout_ms);

#endif /* __UART_H__ */
